/*******************************************************************
 *   > File Name: key.c
 *   > Author: fly
 *   > Mail: XXXXXXXX@icode.com
 *   > Create Time: 2020年04月28日 星期二 10时01分12秒
 ******************************************************************/
#include <types.h>
#include <s5pv210/reg-gpio.h>
#include <s5pv210/reg-others.h>
//#include <s5pv210/hw-api.h>
#include <io.h>
#include "key.h"
#include "led.h"
#include "buzzer.h"

void key_init(void)
{
    /*
     * LEFT ->EINT2   -> GPH0_2
     * DOWN ->EINT3   -> GPH0_3
     * UP   ->KP_COL0 -> GPH2_0
     * RIGHT->KP_COL1 -> GPH2_1
     * BACK ->KP_COL2 -> GPH2_2
     * MENU ->KP_COL3 -> GPH2_3
     */
    /* LEFT */
    writel(S5PV210_GPH2CON, (readl(S5PV210_GPH0CON) & ~(0xf<<8)) | (0x0<<8));
    writel(S5PV210_GPH2PUD, (readl(S5PV210_GPH0PUD) & ~(0x3<<4)) | (0x2<<4));
    /* DOWN */
    writel(S5PV210_GPH2CON, (readl(S5PV210_GPH0CON) & ~(0xf<<12)) | (0x0<<12));
    writel(S5PV210_GPH2PUD, (readl(S5PV210_GPH0PUD) & ~(0x3<<6)) | (0x2<<6));
    /* UP */
    writel(S5PV210_GPH2CON, (readl(S5PV210_GPH2CON) & ~(0xf<<0)) | (0x0<<0));
    writel(S5PV210_GPH2PUD, (readl(S5PV210_GPH2PUD) & ~(0x3<<0)) | (0x2<<0));
    /* RIGHT */
    writel(S5PV210_GPH2CON, (readl(S5PV210_GPH2CON) & ~(0xf<<4))| (0x0<<4));
    writel(S5PV210_GPH2PUD, (readl(S5PV210_GPH2PUD) & ~(0x3<<2)) | (0x2<<2));
    /* BACK */
    writel(S5PV210_GPH2CON, (readl(S5PV210_GPH2CON) & ~(0xf<<8))| (0x0<<8));
    writel(S5PV210_GPH2PUD, (readl(S5PV210_GPH2PUD) & ~(0x3<<4)) | (0x2<<4));
    /* MENU */
    writel(S5PV210_GPH2CON, (readl(S5PV210_GPH2CON) & ~(0xf<<12))| (0x0<<12));
    writel(S5PV210_GPH2PUD, (readl(S5PV210_GPH2PUD) & ~(0x3<<6)) | (0x2<<6));
}

u8_t leftKey_IsPressed(void)
{
	/*
    if(readl(S5PV210_GPH0DAT) & (0x1<<2))   //GPH0_2，LEFT
    {
        return 1;
    }else{
        return 0;
    }
	*/
	
	return ((readl(S5PV210_GPH0DAT) & (0x1<<2))?1:0);
}

u8_t downKey_IsPressed(void)
{
	/*
    if(readl(S5PV210_GPH0DAT) & (0x1<<3))   //GPH0_3，DOWN
    {
        return 1;
    }
    else
    {
        return 0;
    }
	*/
	
	return ((readl(S5PV210_GPH0DAT) & (0x1<<3))?1:0);
}

u8_t upKey_IsPressed(void)
{
	/*
    if(readl(S5PV210_GPH2DAT) & (0x1<<0))   //GPH2_0，UP
    {
        return 1;
    }
    else
    {
        return 0;
    }
	*/
	
	return ((readl(S5PV210_GPH2DAT) & (0x1<<0))?1:0);
}

u8_t rightKey_IsPressed(void)
{
	/*
    if(readl(S5PV210_GPH2DAT) & (0x1<<1))   //GPH2_1，RIGHT
    {
        return 1;
    }
    else
    {
        return 0;
    }
	*/
	
	return ((readl(S5PV210_GPH2DAT) & (0x1<<1))?1:0); 
}

u8_t backKey_IsPressed(void)
{
	/*
    if(readl(S5PV210_GPH2DAT) & (0x1<<2))   //GPH2_2，BACK
    {
        return 1;
    }
    else
    {
        return 0;
    }
	*/
	
	return ((readl(S5PV210_GPH2DAT) & (0x1<<2))?1:0);
}

u8_t menuKey_IsPressed(void)
{
	/*
    if(readl(S5PV210_GPH2DAT) & (0x1<<3))   //GPH2_3，MENU
    {
        return 1;
    }
    else
    {
        return 0;
    }
	*/
	
	return ((readl(S5PV210_GPH2DAT) & (0x1<<3))?1:0);
}

void key_polling(void)
{
    while(1) {
        /* left key action */
        if(leftKey_IsPressed()){
            mdelay(20);
            if(leftKey_IsPressed()){
                led_set(0,1);
            }
        }else{
                led_set(0,0);
        }

        /* down key action */
        if(downKey_IsPressed()){
            mdelay(20);
            if(downKey_IsPressed()){
                led_set(1,1);
            
            }
        }else{
                led_set(1,0);
        
        }

        /* up key action */
        if(upKey_IsPressed()){
            mdelay(20);
            if(upKey_IsPressed()){
                led_set(2,1);
            }
        }else{
                led_set(2,0);
        }

        /* right key action */
        if(rightKey_IsPressed()){
            mdelay(20);
            if(rightKey_IsPressed()){
                led_set(3,1);
            
            }
        }else{
                led_set(3,0);
        }

        /* back key action */
        if(backKey_IsPressed()){
            mdelay(20);
            if(backKey_IsPressed()){
                led_all_on();
            }
        }else{
                led_all_off();
        }

        /* menu key action */
        if(menuKey_IsPressed()){
            mdelay(20);
            if(menuKey_IsPressed()){
                speaker_set(1);
            }
        }else{
                speaker_set(0);
        }
    }
}
